package com.dalor.phonesmanager.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Binder;
import android.os.Build;
import android.support.v4.content.ContextCompat;

import java.util.ArrayList;

/**
 * Created by 吴昶 on 2018/9/13.
 * 权限请求
 */
public class PermissionUtil {

//    /**
//     *  所有权限
//     */
//    public static String[]permissionsREAD={
//            Manifest.permission.READ_EXTERNAL_STORAGE,//sd卡读取权限
//            Manifest.permission.WRITE_EXTERNAL_STORAGE,//SD卡写权限
//            Manifest.permission.READ_PHONE_STATE,//读取手机状态权限
//            Manifest.permission.ACCESS_FINE_LOCATION,//读取手机位置权限
//            Manifest.permission.ACCESS_COARSE_LOCATION,
//            Manifest.permission.CAMERA,   //相机权限
//            Manifest.permission.ACCESS_NETWORK_STATE };

    //相机权限
    public static String[] cameraPermission = {Manifest.permission.CAMERA};

    public static final int GET_PERMISSION=0x1001;
    /**
     *
     * @param permissions
     * @return  false 不需要申请权限   true需要申请权限
     */
    public static boolean getPermission(String[] permissions, Activity activity){
        if(permissions==null||permissions.length==0) return false;
        int pid = Binder.getCallingPid();
        int uid = Binder.getCallingUid();
        ArrayList<String> per=new ArrayList<String>();
        if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.M) {
            for (String permission : permissions) {
                if (activity.checkPermission(permission, pid, uid) != PackageManager.PERMISSION_GRANTED) {
                    per.add(permission);
                }
            }
            if (per.size() > 0) {
                activity.requestPermissions(per.toArray(new String[per.size()]), GET_PERMISSION);
                return true;
            }else {
                return false;
            }
        }else {
            return false;
        }
    }

    /**
     * 判断是否缺少相机权限
     */
    public static boolean lacksPermission(Context mContexts, String permission) {
        return ContextCompat.checkSelfPermission(mContexts, permission) ==
                PackageManager.PERMISSION_DENIED;
    }
}
